let app = getApp() 
Component({
    properties: {
      },
      data:{
        visible1: false,
        actions1: [
            {
                name: '男',
                value:1
            },
            {
                name: '女',
                value:2 
            }
        ],
        array: ['选择性别', '男', '女'],
        defaultAvatar:'../../../static/image/chat-tx.png',
        index:0,
        date: '选择生日',
        birthday: '选择生日',
        province:'选择地区',
        city:'',
        district:'',
        region: ['选择地区', '', ''],
        submitData: {
          user_headimg: "",
          nick_name:"",
          sex: "",
          birthday:"",
          area:"",
          phone:"",
          wechat:"",
          qq:"",
          province:"",
          city:"",
          district:""
      }
      },
    lifetimes: {
         onShow: function () {
          
         },
        attached: function () {
          var timestamp = Date.parse(new Date());
          var date = new Date(timestamp);
          var Y =date.getFullYear();
          var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
          var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
          this.setData({
            endday:Y+'-'+M+'-'+ D
          })
          var that=this
          wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Member_getUserCenter',
                user_id:wx.getStorageSync('user_id')
              },
              success: function(res) {
                     var address=res.data.data.member_data.user_info.province+res.data.data.member_data.user_info.city+res.data.data.member_data.user_info.district
                     that.setData({
                        pageUserInfo:res.data.data.member_data.user_info,
                        index:res.data.data.member_data.user_info.sex,
                        user_headimg:res.data.data.member_data.user_info.user_headimg,
                        nick_name:res.data.data.member_data.user_info.nick_name,
                        qq:res.data.data.member_data.user_info.qq!=null?res.data.data.member_data.user_info.qq:'',
                        wechat:res.data.data.member_data.user_info.wechat!=null?res.data.data.member_data.user_info.wechat:'',
                        phone:res.data.data.member_data.user_info.phone,
                        birthday:res.data.data.member_data.user_info.birthday!=null?res.data.data.member_data.user_info.birthday:'请选择生日',
                        province:res.data.data.member_data.user_info.province,
                        city:res.data.data.member_data.user_info.city,
                        district:res.data.data.member_data.user_info.district,
                        address:res.data.data.member_data.user_info.district!=null?address:'请选择地区'
                     })
              }
            })
       }
      },
    methods: {
      save:function(){
        var that=this
            wx.request({
                url: app.globalData.siteroot,
                data: {
                  do:'Member_userInfoUpdate',
                  user_headimg:that.data.user_headimg,
                  nick_name:typeof(that.data.nick_name)!='undefined'?that.data.nick_name:'',
                  sex:that.data.index,
                  birthday:that.data.birthday,
                  province:that.data.province,
                  city:that.data.city,
                  district:that.data.district,
                  qq:typeof(that.data.qq)!='undefined'?that.data.qq:'',
                  wechat:typeof(that.data.wechat)!='undefined'?that.data.wechat:'',
                  phone:typeof(that.data.phone)!='undefined'?that.data.phone:'',
                  user_id:wx.getStorageSync('user_id')
                },
                success: function(res) {
                  var userinfo=wx.getStorageSync('userinfo')
                  userinfo.user_headimg=that.data.user_headimg
                  userinfo.nick_name=typeof(that.data.nick_name)!='undefined'?that.data.nick_name:''
                  wx.setStorageSync('userinfo',userinfo);
                  wx.showToast({
                    title: '保存成功！',
                    icon: 'success', 
                    duration: 3000 
                 })
                 wx.navigateBack({
                  delta: 1,
                  success: function() {
                  }
                })
                }
              }) 
      },
      upShopLogo: function () {
        var that = this;
        wx.showActionSheet({
        itemList: ['从相册中选择', '拍照'],
        success: function (res) {
        if (!res.cancel) {
        if (res.tapIndex == 0) {
               that.chooseWxImageShop('album');
        } else if (res.tapIndex == 1) {
               that.chooseWxImageShop('camera');
            }
          }
         }
        })
      },
      chooseWxImageShop: function (type) {
        var that = this;
        wx.chooseImage({
        sizeType: ['original', 'compressed'],
        sourceType: [type],
        success: function (res) {
        that.data.user_headimg = res.tempFilePaths[0],
        that.upload_file(res.tempFilePaths[0])
        that.setData({
          user_headimg: res.tempFilePaths[0]
             })
           }
         })
        },
        upload_file: function (filePath) {
          var that = this;
          wx.uploadFile({
          url:app.globalData.siteroot+'&do=Index_uploadFile',
          filePath: filePath,
          name: 'file_upload',
          header: {
          'content-type': 'multipart/form-data'
          }, 
          formData: {
          'do':'Index_uploadFile',
          },
          success: function (res) {
          var data=JSON.parse(res.data)
          that.setData({
            user_headimg:data.info.url,
          });
          },
          fail: function (res) {}
          })
      },
      namechange:function(e){
        if(typeof(e.detail.value)!='undefined'&&e.detail.value!='')
        {
          this.setData({
            nick_name:e.detail.value
           })
        }
     },
      qqchange:function(e){
        if(typeof(e.detail.value)!='undefined')
        {
          this.setData({
            qq:e.detail.value
           })
        }
       
     },
      wechatchange:function(e){
        if(typeof(e.detail.value)!='undefined')
        {
          this.setData({
            wechat:e.detail.value
           })
        }
      },
      bindRegionChange: function (e) {
        this.setData({
          region: e.detail.value,
          province:e.detail.value[0],
          city:e.detail.value[1],
          district:e.detail.value[2],
          address:e.detail.value[0]+e.detail.value[1]+e.detail.value[2]
        })
      },
      bindDateChange: function(e) {
        this.setData({
          date: e.detail.value,
          birthday:e.detail.value
        })
      },
      handleCancel1 () {
        this.setData({
            visible1: false
        });
    },
    bindPickerChange:function(e){
      this.setData({
        index: e.detail.value
      })
    }
    }
});
